<?php	
	session_start();
	header("content-type:text/json;charset=utf-8");
	header("Cache-Control: no-cache");
	
	$type = isset($_GET['type'])? $_GET['type'] : 'weather';

	if($type === 'weather'){
		$city = isset($_GET['cityname'])? $_GET['cityname'] : '贵阳';
		//天气API地址
		$url_weather = "http://apistore.baidu.com/microservice/weather?cityname=".$city;
		
		if(empty($_SESSION['wether_data'])){
			$res = curlData($url_weather);
			if($res_arr = json_decode($res, true)) {
				$res = $res_arr['retData'];
				$_SESSION['wether_data'] = "今日". $res['city']."天气(更新时间：".$res['time'].")：" .$res['weather'].", 温度：".$res['l_tmp']."℃—".$res['h_tmp']."℃,日出：".$res['sunrise'] . ",日落：" .$res['sunset'];
			} else {
				$_SESSION['wether_data'] = "无法获取天气数据，请检查！";
			}
		}
		echo $_SESSION['wether_data'];	
		
	}

	if($type === 'zhaiyan')
	{
		if(!isset($_SESSION['zhaiyan']))
		{
			$rand_id = mt_rand(1, 430);
			$url_zhaiyan = "http://zhaiyan.acman.cn/api/utf8?id=".$rand_id;
			$output = curlData($url_zhaiyan);
			$_SESSION['zhaiyan'] = $output;
			echo json_encode(array(
				'errNum' => 0,
				'errMsg' => 'sucess',
				));
			exit();
		}
		else
		{
			echo json_encode(array(
				'errNum' => 0,
				'errMsg' => 'have store in session',
				));
			exit();
		}

		
	}

function curlData($url)
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	
	$output = curl_exec($ch);
	curl_close($ch);
	return $output;
}
